home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / gs3.53 / pdf_draw.ps < prev    next >
Text File  |  1996-01-10  |  8KB  |  300 lines

  1. %    Copyright (C) 1994, 1995 Aladdin Enterprises.  All rights reserved.
  2.  
  3. % pdf_draw.ps
  4. % PDF drawing operations (graphics, text, and images).
  5.  
  6. % We don't handle the following PDF elements yet (identified by
  7. % page number in the reference manual):
  8. %    style strings (63-64), except in a few known fonts
  9. %    font descriptor resources (71-75), except for MissingWidth
  10. %    text clipping modes (104)
  11. %        What do these mean??
  12.  
  13. /.setlanguagelevel where { pop 2 .setlanguagelevel } if
  14. .currentglobal true .setglobal
  15. /pdfdict where { pop } { /pdfdict 100 dict def } ifelse
  16. GS_PDF_ProcSet begin
  17. pdfdict begin
  18.  
  19. % For simplicity, we use a single interpretation dictionary for all
  20. % PDF graphics operations, even though this is too liberal.
  21. /drawopdict 100 dict def
  22.  
  23. % ================================ Graphics ================================ %
  24.  
  25. % ---------------- Graphics state management ---------------- %
  26.  
  27. drawopdict begin
  28.             % Graphics state stack
  29.   /q { q } def
  30.   /Q { Q } def
  31.             % Graphics state setting
  32.   /cm { cm } def
  33.   /i { i } def
  34.   /J { J } def
  35.   /d { d } def
  36.   /j { j } def
  37.   /w { w } def
  38.   /M { M } def
  39. end
  40.  
  41. % ---------------- Color setting ---------------- %
  42.  
  43. /csgray            % <op> <csop> csgray -
  44.  { Page /Resources pget
  45.     { /ColorSpace knownoget
  46.        { /DefaultGray knownoget { 3 -1 roll exch 3 1 roll } if }
  47.       if
  48.     }
  49.    if pop cvx exec
  50.  } bdef
  51. /csrgb            % <op> <csop> csrgb -
  52.  { Page /Resources pget
  53.     { /ColorSpace knownoget
  54.        { /DefaultRGB knownoget { 3 -1 roll exch 3 1 roll } if }
  55.       if
  56.     }
  57.    if pop cvx exec
  58.  } bdef
  59. /cscmyk            % <op> <csop> cscmyk -
  60.  { pop cvx exec
  61.  } bdef
  62. /csresolve        % <csresource> csresolve <colorspace>
  63.  { Page /Resources pget not { << >> } if
  64.    /ColorSpace oget exch oget resolvecolorspace
  65.    dup type /nametype ne { dup length 1 eq { 0 get } if } if
  66.  } bdef
  67. /cset        % <c0> ... [- <sc1> - <sc3> <sc4>] <colorspace> cset -
  68.  { gsave setcolorspace mark currentcolor counttomark
  69.    grestore dup 2 add 1 roll cleartomark get exec
  70.  } bdef
  71.  
  72. %**************** sc and SC don't work, because the color space information
  73. %**************** isn't available at conversion time.
  74. drawopdict begin
  75.   /g { /g { cs sc1 } csgray } bdef
  76.   /rg { /rg { cs sc3 } csrgb } bdef
  77.   /k { /k { cs sc4 } cscmyk } bdef
  78.   /cs { csresolve cs } bdef
  79.   /sc { { null sc1 null sc3 sc4 } FillColorSpace cset } bdef
  80.   /G { /G { CS SC1 } csgray } bdef
  81.   /RG { /RG { CS SC3 } csrgb } bdef
  82.   /K { /K { CS SC4 } cscmyk } bdef
  83.   /CS { csresolve CS } bdef
  84.   /SC { { null SC1 null SC3 SC4 } StrokeColorSpace cset } bdef
  85. end
  86.  
  87. % ---------------- Paths ---------------- %
  88.  
  89. drawopdict begin
  90.             % Path construction
  91.   /m { m } def
  92.   /l { l } def
  93.   /c { c } def
  94.   /v { v } def
  95.   /y { y } def
  96.   /re { re } def
  97.   /h { h } def
  98.             % Path painting and clipping
  99.   /n { n } def
  100.   /S { S } def
  101.   /s { s } def
  102.   /f { f } def
  103.   /f* { f* } def
  104.   /B { B } def
  105.   /b { b } def
  106.   /B* { B* } def
  107.   /b* { b* } def
  108.   /W { W } def
  109.   /W* { W* } def
  110. end
  111.  
  112. % ---------------- XObjects ---------------- %
  113.  
  114. /xobjectprocs mark        % <dict> -proc- -
  115.   /Image { DoImage }
  116.   /Form { DoForm }
  117. .dicttomark readonly def
  118.  
  119. /ncompdict mark
  120.   /DeviceGray 1
  121.   /CalGray 1
  122.   /DeviceRGB 3
  123.   /CalRGB 3
  124.   /Lab 3
  125.   /DeviceCMYK 4
  126.   /CalCMYK 4
  127. .dicttomark readonly def
  128. /defaultdecodedict mark
  129.   /DeviceGray [0 1] readonly
  130.   /CalGray 1 index
  131.   /DeviceRGB [0 1 0 1 0 1] readonly
  132.   /CalRGB 1 index
  133.   /Lab
  134.    { 0 100 2 index 1 get /Range .knownget not { {-100 100 -100 100} } if
  135.      aload pop 6 array astore readonly
  136.    } bind
  137.   /DeviceCMYK [0 1 0 1 0 1 0 1] readonly
  138.   /CalCMYK 1 index
  139. .dicttomark readonly def
  140. /resolvecolorspace    % <cspace> resolvecolorspace <cspace>
  141.  { dup type /arraytype eq
  142.     { dup 0 get /Indexed eq
  143.        { dup 3 oget dup type /stringtype eq
  144.       { pop
  145.       }
  146.       {    % The color lookup table is a stream.
  147.         % Get its contents.
  148.         true resolvestream
  149.         1 index 2 get 1 add
  150.         ncompdict 3 index 1 get get mul
  151.         string readstring pop
  152.         1 index 3 3 -1 roll put
  153.       }
  154.      ifelse
  155.        }
  156.       if
  157.     }
  158.    if
  159.  } bdef
  160. /DoImage
  161.  { dup length dict
  162.    1 index /ColorSpace knownoget
  163.     { resolvecolorspace
  164.       dup type /arraytype eq { dup length 1 eq { 0 get } if } if
  165.       exch begin /ColorSpace exch def
  166.     }
  167.     { begin
  168.     }
  169.    ifelse
  170.    /ImageType 1 def
  171.         % Always define ImageMask appropriately.
  172.    dup /ImageMask knownoget dup { and } if
  173.      /ImageMask exch def
  174.    /Width 2 copy oget def
  175.    /Height 2 copy oget def
  176.    /BitsPerComponent 2 copy oget def
  177.    /Decode 2 copy knownoget not
  178.     {        % Decode is required for the PostScript image operators.
  179.       ImageMask
  180.        { [0 1]
  181.        }
  182.        { ColorSpace dup type /arraytype eq { 0 get } if dup /Indexed eq
  183.       { pop [ 0 1 BitsPerComponent bitshift 1 sub ] }
  184.       { defaultdecodedict exch get exec }
  185.      ifelse
  186.        }
  187.       ifelse
  188.     }
  189.    if def
  190.    /Interpolate 2 copy knownoget { def } { pop } ifelse
  191.    /ImageMatrix Width 0 0 Height neg 0 Height 6 array astore def
  192.         % Define DataSource as the width of the row buffer,
  193.         % which is what is needed if we're writing PostScript.
  194.    /DataSource 
  195.      Width BitsPerComponent mul
  196.      ImageMask not { Decode length 2 idiv mul } if
  197.      7 add 8 idiv
  198.    def
  199.         % Even though we're going to read data,
  200.         % pass false to resolvestream so that
  201.         % it doesn't try to use Length (which may not be present).
  202.    false resolvestream /Is_stream exch store
  203.    currentdict end ID
  204.  } bdef
  205. % Redefine Is, which constructs the data source for the image,
  206. % to retrieve the stream.  (pdf_2ps.ps redefines Is to copy the data too.)
  207. userdict /Is_stream null put
  208. /Is        % <imagedict> Is <imagedict> <datasource>
  209.  { Is_stream
  210.  } bdef
  211.  
  212. /DoForm
  213.  { dup [ /pop load 2 index
  214.     { false resolvestream pdfopdict .pdfrun }
  215.    aload pop ] cvx /PaintProc exch put
  216.    execform
  217.  } bdef
  218.  
  219. drawopdict begin
  220.   /Do
  221.     { PDFfile fileposition exch
  222.       Page /Resources pget not { << >> } if
  223.       /XObject oget exch oget
  224.       dup /Subtype get xobjectprocs exch get exec
  225.       PDFfile exch setfileposition
  226.     } bdef
  227. end
  228.  
  229. % ---------------- In-line images ---------------- %
  230.  
  231. % Undo the abbreviations in an in-line image dictionary.
  232. % Note that these can appear as keys, values, or members of array values.
  233. % /I is ambiguous; we check specially for it below.
  234. /unabbrevdict mark
  235.     % Top-level dictionary keys
  236.   /BPC /BitsPerComponent  /CS /ColorSpace  /D /Decode  /DP /DecodeParms
  237.   /F /Filter  /H /Height  /IM /ImageMask  /W /Width
  238.     % Values
  239.   /AHx /ASCIIHexDecode  /A85 /ASCII85Decode  /CC /CalCMYK
  240.   /CCF /CCITTFaxDecode  /CG /CalGray  /CR /CalRGB
  241.   /DCT /DCTDecode  /CMYK /DeviceCMYK  /G /DeviceGray  /RGB /DeviceRGB
  242.   /I /Indexed  /LZW /LZWDecode  /RL /RunLengthDecode
  243. .dicttomark readonly def
  244. /unabbrev        % <obj> unabbrev <obj'>
  245.  { dup type /nametype eq
  246.     { unabbrevdict 1 index .knownget { exch pop } if
  247.     }
  248.     { dup type /arraytype eq
  249.        { dup 0 1 2 index length 1 sub
  250.       { 2 copy get unabbrev put dup
  251.       }
  252.      for pop
  253.        }
  254.       if
  255.     }
  256.    ifelse
  257.  } bdef
  258.  
  259. drawopdict begin
  260.   /BI { mark } bdef
  261.   /ID
  262.     { counttomark
  263.        { counttomark 1 roll
  264.      dup /I eq { pop /Interpolate } { unabbrev } ifelse
  265.        }
  266.       repeat
  267.       /File PDFsource
  268.       .dicttomark DoImage
  269.       PDFsource token pop /EI ne { /ID cvx /syntaxerror signalerror } if
  270.     } bdef
  271. end
  272.  
  273. % ================================ Text ================================ %
  274.  
  275. drawopdict begin
  276.             % Text control
  277.   /BT { BT } def
  278.   /ET { ET } def
  279.   /Tc { Tc } def
  280.   /TL { TL } def
  281.   /Tr { Tr } def
  282.   /Ts { Ts } def
  283.   /Tw { Tw } def
  284.   /Tz { Tz } def
  285.             % Text positioning
  286.   /Td { Td } def
  287.   /TD { TD } def
  288.   /Tm { Tm } def
  289.   /T* { T* } def
  290.             % Text painting
  291.   /Tj { Tj } def
  292.   /' { ' } def
  293.   /" { " } def
  294.   /TJ { TJ } def
  295. end
  296.  
  297. end            % pdfdict
  298. end            % GS_PDF_ProcSet
  299. .setglobal
  300.